package com.study.lxpstudy.service;

import com.study.lxpstudy.dao.UserDao;
import com.study.lxpstudy.pojo.User;
import com.study.lxpstudy.pojo.dto.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service // spring的bean
public class UserService implements IUserService {
    @Autowired
    UserDao userDao;

    @Override
    public User add(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        // 调用数据访问类
        return userDao.save(userPojo);
    }

    @Override
    public User getUser(Integer userId) {
        return userDao.findById(userId).orElseThrow(() -> new IllegalArgumentException("用户不存在，参数异常!"));
    }

    @Override
    public User put(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        return userDao.save(userPojo);
    }

    @Override
    public void delete(Integer userId) {
        userDao.deleteById(userId);
    }

    @Override
    public Iterable<User> list() {
        return userDao.findAll();
    }
}
